package droid.engine;

import android.view.*;
import android.content.*;
import android.graphics.*;

public class Panel extends SurfaceView implements SurfaceHolder.Callback {
	
	private GraphicsThread thread;
	private int x = 20;
	private int y = 20;
	
	public Panel(Context context) {
		super(context);
		getHolder().addCallback(this);
		thread = new GraphicsThread(getHolder(), this);
		setFocusable(true);
	}

	@Override
	public void onDraw(Canvas canvas) {		
		Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
		canvas.drawColor(Color.BLACK);
		canvas.drawBitmap(bitmap, x - bitmap.getWidth()/2, y - bitmap.getHeight()/2, null);
	}
	
	public boolean onTouchEvent(MotionEvent event) {
		x = (int)event.getX();
		y = (int)event.getY();
		return true;
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
		
	}

	public void surfaceCreated(SurfaceHolder holder) {
		thread.setRunning(true);
		thread.start();
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		boolean retry = true;
		thread.setRunning(false);
		while (retry) {
			try {
				thread.join();
				retry = false;
			}
			catch (InterruptedException e) {
				// Do nothing. Keep retrying.
			}
		}
	}
}
